package org.dtrd.modules.record.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.record.entity.bean.DailyDietRecordBean;
import org.dtrd.modules.record.entity.bean.DietRecordBean;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.IFoodRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;

/**
 * @Author: QR
 * @Date: 2021/10/15-14:01
 */
@Api(tags = "饮食打卡记录")
@RestController
@RequestMapping("/101/02")
@Slf4j
public class FoodRecordController extends BaseController {

    @Autowired
    private IFoodRecordService foodRecordService;

    @ApiOperation("添加或修改饮食记录")
    @PostMapping("/1010201")
    @AutoLog(value="添加或修改饮食记录", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveFoodRecord(@RequestBody DietRecordBean dietRecordBean) {
        if (StrUtil.isBlank(dietRecordBean.getRecordTime())
                // || StrUtil.isEmpty(dietRecordBean.getRecordPhoto()) 图片改为选填 2022年09月21日14:23:05
                || dietRecordBean.getPatientId() == null
                || dietRecordBean.getFoodRecordType() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        boolean result = foodRecordService.saveFoodRecord(dietRecordBean);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_REQUEST_FAILURE);
    }

    @ApiOperation(value = "按日期分组获取患者饮食记录")
    @PostMapping("/1010202")
    @AutoLog(value="按日期分组获取患者饮食记录", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodRecordList(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            Integer page = getIntParam(request.getPage(), 1);
            Integer pageSize = getIntParam(request.getPageSize(), 20);
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            Integer count = foodRecordService.getFoodRecordDateCount(patientId, startDate, endDate);
            JSONObject result = new JSONObject();
            if (count != null && count > 0) {
                List<DailyDietRecordBean> list = foodRecordService.getAllGroupedFoodRecord(patientId, startDate, endDate, page, pageSize);
                result.put("recordList", list);
                result.put("totalPage", count <= pageSize || pageSize <= 0 ? 1 : setTotalPage(pageSize, count));
                result.put("count", count);
            }else {
                result.put("recordList", Collections.EMPTY_LIST);
                result.put("totalPage", 1);
                result.put("count", 0);
            }
            return Result.OK(result);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

//    @ApiOperation(value = "按日期分组获取患者饮食记录", notes = "返回的对象是每天一条")
//    @PostMapping("/1010203")
//    @AutoLog(value="按日期分组获取患者饮食记录", operateType = OPERATE_TYPE_QUERY)
//    public Result<?> getFoodList(@RequestBody TimePeriodRequest request) {
//        if (request.getPatientId() == null) {
//            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
//        }
//        int type = getIntParam(request.getType(), 1);
//        if (type == 1 || type == 3) {
//            String startDate = null;
//            String endDate = null;
//            String[] timePeriod = handleTimePeriod(request);
//            if (timePeriod != null) {
//                startDate = timePeriod[0];
//                endDate = timePeriod[1];
//            }
//            if (startDate == null || endDate == null) {
//                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
//            }
//            Integer patientId = request.getPatientId();
//            JSONObject result = new JSONObject();
//            List<DietListInfo> list = foodRecordService.getFoodRecordList(patientId, startDate, endDate);
//            result.put("recordList", list);
//            result.put("count", list.size());
//            return Result.OK(result);
//        }
//        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
//    }

}
